﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Controls;

namespace Pavilion.NodeDesigner
{
    public class ConnectionCreator
    {
        public Anchor TryCreatingConnection(Visual reference, Point location, Anchor start)
        {
            Anchor anchor = null;
            var adornedElement = reference as UIElement;

            VisualTreeHelper.HitTest(reference, null,
                hitTestParameters: new PointHitTestParameters(location),
                resultCallback: (HitTestResult result) =>
                {
                    anchor = NewWayHelper.GetAncestor(result.VisualHit, typeof(Anchor)) as Anchor;
                    if (anchor != null)
                        AdornerLayer.GetAdornerLayer(reference).Add(new Connection(adornedElement, start, anchor));               

                    return HitTestResultBehavior.Stop;
                });
            return anchor;
        }
    }
}
